﻿using HML.Design;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace HML
{
    /// <summary>
    /// 头像标签控件
    /// </summary>
    [Description("头像标签控件")]
    [ToolboxItem(true)]
    [DefaultProperty("Image")]
    [TypeConverter(typeof(PropertyOrderConverter))]
    [Designer(typeof(HeadTagDesigner))]
    [ToolboxBitmap(typeof(HeadTag), "Controls.HeadTag.HeadTag.bmp")]
    public class HeadTag : DpiControl
    {
        #region 停用事件

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler PaddingChanged
        {
            add { base.PaddingChanged += value; }
            remove { base.PaddingChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TabIndexChanged
        {
            add { base.TabIndexChanged += value; }
            remove { base.TabIndexChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TabStopChanged
        {
            add { base.TabStopChanged += value; }
            remove { base.TabStopChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler RightToLeftChanged
        {
            add { base.RightToLeftChanged += value; }
            remove { base.RightToLeftChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ImeModeChanged
        {
            add { base.ImeModeChanged += value; }
            remove { base.ImeModeChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackColorChanged
        {
            add { base.BackColorChanged += value; }
            remove { base.BackColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TextChanged
        {
            add { base.TextChanged += value; }
            remove { base.TextChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ForeColorChanged
        {
            add { base.ForeColorChanged += value; }
            remove { base.ForeColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackgroundImageChanged
        {
            add { base.BackgroundImageChanged += value; }
            remove { base.BackgroundImageChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackgroundImageLayoutChanged
        {
            add { base.BackgroundImageLayoutChanged += value; }
            remove { base.BackgroundImageLayoutChanged -= value; }
        }

        #endregion

        #region 新增属性

        private Color borderColor = Color.FromArgb(110, 153, 153);
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Description("边框颜色")]
        [Category("杂项")]
        [PropertyOrder(-200)]
        [DefaultValue(typeof(Color), "110, 153, 153")]
        public Color BorderColor
        {
            get { return this.borderColor; }
            set
            {
                if (this.borderColor == value)
                    return;

                this.borderColor = value;
                this.Invalidate();
            }
        }

        private Color contentColor = Color.FromArgb(153, 204, 204);
        /// <summary>
        /// 背景颜色
        /// </summary>
        [Description("背景颜色")]
        [Category("杂项")]
        [PropertyOrder(-198)]
        [DefaultValue(typeof(Color), "153, 204, 204")]
        public Color ContentColor
        {
            get { return this.contentColor; }
            set
            {
                if (this.contentColor == value)
                    return;

                this.contentColor = value;
                this.Invalidate();
            }
        }

        private Image image = null;
        /// <summary>
        /// 头像图片
        /// </summary>
        [Description("头像图片")]
        [Category("杂项")]
        [PropertyOrder(-194)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image Image
        {
            get { return this.image; }
            set
            {
                if (this.image == value)
                    return;

                this.image = value;
                this.Invalidate();
            }
        }

        #endregion

        #region 重写属性

        protected override Size DefaultSize
        {
            get { return new Size(50, 50); }
        }

        protected override ImeMode DefaultImeMode
        {
            get { return ImeMode.Disable; }
        }

        #endregion

        #region 停用属性

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Padding Padding
        {
            get { return base.Padding; }
            set { base.Padding = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new int TabIndex
        {
            get { return base.TabIndex; }
            set { base.TabIndex = value; }
        }

        [DefaultValue(false)]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool TabStop
        {
            get { return base.TabStop; }
            set { base.TabStop = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Font Font
        {
            get { return base.Font; }
            set { base.Font = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set { base.ForeColor = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override RightToLeft RightToLeft
        {
            get { return base.RightToLeft; }
            set { base.RightToLeft = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new ImeMode ImeMode
        {
            get { return base.ImeMode; }
            set { base.ImeMode = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Color BackColor
        {
            get { return base.BackColor; }
            set { base.BackColor = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Image BackgroundImage
        {
            get { return base.BackgroundImage; }
            set { base.BackgroundImage = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new ImageLayout BackgroundImageLayout
        {
            get { return base.BackgroundImageLayout; }
            set { base.BackgroundImageLayout = value; }
        }

        #endregion

        #region 字段

        /// <summary>
        /// 边框
        /// </summary>
        private int borderThickness = 1;

        #endregion

        public HeadTag()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.SuspendLayout();
            this.BackColor = Color.Transparent;
            this.TabStop = false;
            this.SetControlShape();
            this.ResumeLayout();
        }

        #region 重写

        protected override void OnScaleDpiChangedInitialize()
        {
            this.SetControlShape();
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            Rectangle content_rect = new Rectangle(this.ClientRectangle.X + this.borderThickness * 2, this.ClientRectangle.Y + this.borderThickness * 2, this.ClientRectangle.Width - this.borderThickness * 2 * 2, this.ClientRectangle.Height - this.borderThickness * 2 * 2);

            //背景
            SolidBrush back_sb = new SolidBrush(ControlHelper.ConvertToAutoColor(this, this.ContentColor));
            g.FillEllipse(back_sb, content_rect);
            back_sb.Dispose();

            //图标
            if (this.Image != null)
            {
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(content_rect);
                g.SetClip(gp);
                g.DrawImage(this.Image, content_rect);
                g.ResetClip();
                gp.Dispose();
            }

            //边框
            Pen border_pen = new Pen(ControlHelper.ConvertToAutoColor(this, this.BorderColor)) { Alignment = PenAlignment.Outset };
            g.DrawEllipse(border_pen, content_rect);
            border_pen.Dispose();

        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (!this.Focused)
            {
                this.Focus();
            }

            base.OnMouseDown(e);
        }

        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            if (this.Width == width && this.Height != height)
            {
                width = height;
            }
            else
            {
                height = width;
            }

            base.SetBoundsCore(x, y, height, height, specified);

            this.SetControlShape();
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 设置控件形状
        /// </summary>
        private void SetControlShape()
        {
            GraphicsPath gp = new GraphicsPath();
            int diameter = Math.Max(0, this.Width - this.borderThickness * 2);
            gp.AddEllipse(new RectangleF(this.ClientRectangle.X + this.borderThickness, this.ClientRectangle.Y + this.borderThickness, diameter, diameter));

            this.Region = new Region(gp);
            gp.Dispose();
        }

        #endregion
    }
}
